<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/7/15
 * Time: 9:33
 */

namespace pay\modules\backend\controllers;


use pay\modules\backend\models\Application;
use pay\modules\backend\models\CommonApp;
use pay\libs\Tools;
use Yii;

/** 支付应用控制器
 * Class PaymentApplicationController
 * @package backend\controllers
 */
class PaymentApplicationController extends BasicController
{
    /*** 支付应用
     * @return false|string
     */
    public function actionList()
    {
        $get = Yii::$app->request->get();
        $appId = isset($get['appId'])?$get['appId']:'';
        $status = isset($get['status'])?$get['status']:'';
        $page = isset($get['page'])?$get['page']:1;
        if (empty($appId)){
            return  Tools::showRes(99,'参数有误');
        }

        if  (!empty($status)){
            $where = ['status' => $status,''];
        }else{
            $where = '';
        }

        $pageSize = Yii::$app->params['pageSize'];

        $app_id = $this->getAppId($appId);
        $searchModel = Application::find()->where($where)->andWhere(['app_id' => $app_id]);
        $count = $searchModel->count();
        $appLicationList = $searchModel->offset($pageSize * ($page - 1))->select(['aid','app_id','a_key','name','remake','status','create_time'])->asArray()->limit($pageSize)->all();

        return Tools::showRes(0,'success',[
            'appLicationList' => $appLicationList,
            'pageInfo' => [
                'pageSize' => $pageSize,
                'page' => $page,
                'count' => $count,
            ]
        ]);
    }

    /*** 添加，修改支付应用
     * @return false|string
     */
    public function actionCreateApplication()
    {

        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();

            if (isset($post['appId']) and !empty($post['appId'])){
                $post['app_id'] = $this->getAppId($post['appId']);
            }else{
                return Tools::showRes(99,'参数有误');
            }
            if (!isset($post['name']) || empty($post['name'])){
                return Tools::showRes(99,'应用名称不能为空');
            }
            if (!isset($post['app_id']) || empty($post['app_id'])){
                return Tools::showRes(99,'实例id不能为空');
            }

            $applicationModel = new Application();

            if ($applicationModel->addInfo($post)){
                $this->setSysLog('添加/修改支付应用'.$post['name'].'成功');
                return Tools::showRes(0,'success');
            }
            $this->setSysLog('添加/修改支付应用'.$post['name'].'失败');
            return Tools::showRes(99,'error');

        }


        $get = Yii::$app->request->get();
        $aid = isset($get['aid'])?(int)$get['aid']:'';
        if (!empty($aid)){
            $appLicationInfo = Application::find()->where('aid = :id',[':id' => $aid])->select(['aid','name','remake','status'])->asArray()->one();

            if (!empty($appLicationInfo)){
                return Tools::showRes(0,'success',[
                    'info' => $appLicationInfo,
                ]);
            }
        }
        return Tools::showRes(0,'success',[
            'info' => ''
        ]);
    }
    
}